package com.etoak.common.jwt;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class MyJwtUtil {
    public static final String KEY_STRING = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

    /**
     * 签名秘钥
     */
    public static final Key SIGN_KEY = Keys.hmacShaKeyFor(KEY_STRING.getBytes());

    public static final long EXPIRE = 1000 * 60 * 60 * 12;

    public static String create(Map<String,Object> claims) {
        // 签发时间
        Date now = new Date();

        return Jwts.builder()
                .signWith(SIGN_KEY) //签名key
                .setClaims(claims) //自定义 payload
                .setIssuedAt(now)  //payload: "iat"
                .setExpiration(new Date(now.getTime() + EXPIRE)) //payload: "exp"
                .compact();
    }

    /**
     * 解析 token
     */
    public static Map<String,Object> parse(String token) {
        return Jwts.parserBuilder()
                .setSigningKey(SIGN_KEY)
                .build()
                .parseClaimsJws(token)
                .getBody();
    }
}
